package com.iraeta.service.etl;

import com.iraeta.dao.erpgq.GqCurrentStockDao;
import com.iraeta.dao.erpgq.GqRdRecord32Dao;
import com.iraeta.dao.erpgq.GqSoMainDao;
import com.iraeta.dao.main.ErpgqCurrentstockDao;
import com.iraeta.dao.main.ErpgqRdrecord32Dao;
import com.iraeta.dao.main.ErpgqSomainDao;
import com.iraeta.dao.main.EtllogDao;
import com.iraeta.entity.main.ErpgqCurrentstock;
import com.iraeta.entity.main.ErpgqRdrecord32;
import com.iraeta.entity.main.ErpgqSomain;
import com.iraeta.entity.main.Etllog;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ErpGqEtlService {
    @Autowired
    EtllogDao etllogDao;
    @Autowired
    GqSoMainDao gqSoMainDao;
    @Autowired
    ErpgqSomainDao erpgqSomainDao;
    @Autowired
    GqRdRecord32Dao gqRdRecord32Dao;
    @Autowired
    ErpgqRdrecord32Dao erpgqRdrecord32Dao;
    @Autowired
    GqCurrentStockDao gqCurrentStockDao;
    @Autowired
    ErpgqCurrentstockDao erpgqCurrentstockDao;

    public void processErpGqSoMain(){
        List<HashMap<String, Object>> res = gqSoMainDao.listSoMain();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;

        for (HashMap<String, Object> map : res) {
            try {
                ErpgqSomain item = new ErpgqSomain();
                Util.mapToBean(map, item);
                ErpgqSomain erpgqSomainDb = erpgqSomainDao.selectByPrimaryKey(item.getAutoid());
                if(erpgqSomainDb == null){
                    erpgqSomainDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpgqSomainDb)){
                        erpgqSomainDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        int deleteCount = 0;
        try {
            List<Integer> erpIdList = res.stream().map(x->(Integer)x.get("AutoID")).collect(Collectors.toList());
            List<ErpgqSomain> erpgqSomainList = erpgqSomainDao.selectTopRecord();
            List<ErpgqSomain> filterErpgqSomainList = erpgqSomainList.stream().filter(x->  !erpIdList.contains(x.getAutoid().intValue()) ).collect(Collectors.toList());
            deleteCount = filterErpgqSomainList.size();
            for (ErpgqSomain erpgqSomain : filterErpgqSomainList) {
                erpgqSomainDao.deleteByPrimaryKey(erpgqSomain.getAutoid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("钢球ERP销售订单", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("钢球ERP销售订单新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }

    public void processErpgqRdrecord32(){
        List<HashMap<String, Object>> res = gqRdRecord32Dao.listRdRecord32();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                ErpgqRdrecord32 item = new ErpgqRdrecord32();
                Util.mapToBean(map, item);
                ErpgqRdrecord32 erpRdrecord32Db = erpgqRdrecord32Dao.selectByPrimaryKey(item.getAutoid());
                if(erpRdrecord32Db == null){
                    erpgqRdrecord32Dao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpRdrecord32Db)){
                        erpgqRdrecord32Dao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }

        //处理删除的数据
        int deleteCount = 0;
        try {
            List<Long> erpIdList = res.stream().map(x->(Long)x.get("AutoID")).collect(Collectors.toList());
            List<ErpgqRdrecord32> erpgqRdrecord32List = erpgqRdrecord32Dao.selectTopRecord();
            List<ErpgqRdrecord32> filterErpgqRdrecord32List = erpgqRdrecord32List.stream().filter(x->  !erpIdList.contains(x.getAutoid()) ).collect(Collectors.toList());
            deleteCount = filterErpgqRdrecord32List.size();
            for (ErpgqRdrecord32 erpgqRdrecord32 : filterErpgqRdrecord32List) {
                erpgqRdrecord32Dao.deleteByPrimaryKey(erpgqRdrecord32.getAutoid());
            }
        }catch (Exception e){
            exceptionCount++;
        }
        Etllog etllog = new Etllog("钢球ERP销售出库单", insertCount, updateCount, exceptionCount, deleteCount);
        etllogDao.insert(etllog);
        System.out.println("钢球ERP销售出库单新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + deleteCount);
    }

    public void processErpGqCurrentStock(){
        List<HashMap<String, Object>> res = gqCurrentStockDao.listCurrentStock();
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;

        for (HashMap<String, Object> map : res) {
            try {
                ErpgqCurrentstock item = new ErpgqCurrentstock();
                Util.mapToBean(map, item);
                ErpgqCurrentstock erpgqCurrentstockDb = erpgqCurrentstockDao.selectByPrimaryKey(item.getCinvcode());
                if(erpgqCurrentstockDb == null){
                    erpgqCurrentstockDao.insert(item);
                    insertCount++;
                }else{
                    if(!item.equals(erpgqCurrentstockDb)){
                        erpgqCurrentstockDao.updateByPrimaryKey(item);
                        updateCount++;
                    }
                }
            }catch (Exception e){
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("钢球ERP库存数据", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        System.out.println("钢球ERP库存数据新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + 0);
    }
}
